bpo-37064: add option -k to Tools/scripts/pathfix.py#15548
Conversation
- with this option it is possible to keep flags when changing shebang line
| new_interpreter = None | ||
| preserve_timestamps = False | ||
| create_backup = True | ||
| add_flag = None |
|
|
||
| def parse_shebang(shebangline): | ||
| end = len(shebangline) | ||
| start = shebangline.find(b' -', 0, end) # .find() returns index at space |
There was a problem hiding this comment.
Please remove the comment, IMHO it's not really helpful.
", 0, end" are the default value and so can be removed.
|
|
||
|
|
||
| def parse_shebang(shebangline): | ||
| end = len(shebangline) |
There was a problem hiding this comment.
Please add a comment that on Linux, the first whitespace (U+0020) indicates the beginning of options.
| start = shebangline.find(b' -', 0, end) # .find() returns index at space | ||
| if start == -1: | ||
| return b'', b'' | ||
| print(shebangline[(start + 2):end]) |
There was a problem hiding this comment.
Don't commit debug print please (remove it).
|
|
||
|
|
||
| def fixline(line): | ||
| global keep_flags |
There was a problem hiding this comment.
I don't think that this global statement is useful. Variables are global by default.
| if keep_flags: | ||
| flags = parse_shebang(line) | ||
| return b'#! ' + new_interpreter + b" -" + flags | ||
| return b'#! ' + new_interpreter + b'\n' |
There was a problem hiding this comment.
I would prefer to have:
if keep_flags:
flags = parse_shebang(line)
else:
flags = b''
return b'#! ' + new_interpreter + flags + b'\n'
So parse_shebang() must remove the trailing newline.
There was a problem hiding this comment.
I have zero idea how I deleted. In editor I still have:
return b'#! ' + new_interpreter + b' -' + flags + b'\n'
| subprocess.call([sys.executable, self.script, '-i', '/usr/bin/python', '-n', self.temp_file]) | ||
| with open(self.temp_file) as f: | ||
| output = f.read() | ||
| self.assertEqual(output, '#! /usr/bin/python\n' + 'print("Hello world")\n') |
There was a problem hiding this comment.
Please add a test using -k when the shebang does not contain any flag.
| self.temp_file = support.TESTFN | ||
|
|
||
| with open(self.temp_file, 'w') as f: | ||
| f.write('#! /usr/bin/env python -R\n' + 'print("Hello world")\n') |
There was a problem hiding this comment.
I would prefer to have an helper method which takes two arguments: (shebang, pathfix_flags) and return the new shebang. To get tests which look like:
def test_keep_flags(self):
shebang = '#! /usr/bin/env python -R'
self.assertEqual(
self.pathfix(shebang, ['-i', '/usr/bin/python']),
'#! /usr/bin/python')
self.assertEqual(
self.pathfix(shebang, ['-i', '/usr/bin/python', '-k']),
'#! /usr/bin/python -R')
|
|
||
| @classmethod | ||
| def setupUpClass(cls): | ||
| self.addCleanup(support.unlink, support.TESTFN) |
There was a problem hiding this comment.
It doesn't work: self doesn't exist. You may use setUp().
| @@ -0,0 +1 @@ | |||
| Add flag -k to pathscript.py. This flag preserve flags from shebang before change and adds them after change. No newline at end of file | |||
There was a problem hiding this comment.
I suggest: "Add flag -k to pathscript.py script: preserve shebang flags."
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
| @@ -0,0 +1,48 @@ | |||
| import unittest, os, subprocess | |||
There was a problem hiding this comment.
Please put one import per line: see PEP 8.
Try also to sort imports.
| subprocess.call([sys.executable, self.script, *pathfix_flags, self.temp_file]) | ||
| with open(self.temp_file) as f: | ||
| output = f.read() | ||
| print(output) |
There was a problem hiding this comment.
Don't forget to remove debug print when you submit a PR.
| self.addCleanup(support.unlink, support.TESTFN) | ||
|
|
||
| def pathfix(self, shebang, pathfix_flags): | ||
| with open(self.temp_file, 'w') as f: |
There was a problem hiding this comment.
I would prefer to specify the encoding: pass encoding="utf8" to this open, and the other one below.
| self.assertEqual( | ||
| self.pathfix( | ||
| '#! /usr/bin/env python -R', | ||
| ['-i', '/usr/bin/python', '-k', '-n']), |
There was a problem hiding this comment.
Please remove -n from tests and always put it in pathfix() function.
| self.pathfix( | ||
| '#! /usr/bin/env python -R', | ||
| ['-i', '/usr/bin/python', '-k', '-n']), | ||
| '#! /usr/bin/python -R\n' + 'print("Hello world")\n' |
There was a problem hiding this comment.
I don't think that it's useful to check for 'print("Hello world")\n' in each test. I would prefer to test it in pathfix() and just return the first line.
Something like this in pathfix():
lines = output.split('\n')
self.assertEqual(lines[1:], ['print("Hello world")\n']
shebang = lines[0][:-1] # strip the newline
return shebang
vstinner
left a comment
There was a problem hiding this comment.
Thanks, the code now looks way better! A few more comments.
| shebang = lines[0][:-1] # strip the newline | ||
| return shebang | ||
|
|
||
| def test_pathfix_keeping_flags(self): |
There was a problem hiding this comment.
I would prefer:
- put test_pathfix_without_keeping_flags() code into test_pathfix()
- move test_pathfix_keeping_flags() after test_pathfix()
- add a test in test_pathfix_keeping_flags(): use -k but the shebang doesn't contain any flag (the bug that you just fixed)
| def parse_shebang(shebangline): | ||
| shebangline = shebangline.strip() | ||
| end = len(shebangline) | ||
| start = shebangline.find(b' -', 0, end) |
There was a problem hiding this comment.
"end" argument is useless: shebangline.find(b' -') behaves the same and is simpler. Remove 0 and end.
| if keep_flags: | ||
| flags = parse_shebang(line) | ||
| return b'#! ' + new_interpreter + b" -" + flags | ||
| return b'#! ' + new_interpreter + b'\n' |
Add flag -k to pathscript.py script: preserve shebang flags.
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548) Add flag -k to pathscript.py script: preserve shebang flags. (cherry picked from commit 50254ac) * bpo-37064: Add option -a to pathfix.py tool (GH-15717) Add option -a to Tools/Scripts/pathfix.py script: add flags. (cherry picked from commit 1dc1acb)
Add flag -k to pathscript.py script: preserve shebang flags.
Add flag -k to pathscript.py script: preserve shebang flags.
https://bugs.python.org/issue37064